#include "anim.h"
#include "TPS929120.h"
#include "pwm_pal1.h"
#include "mcu.h"

uint16_t breath_duty;
uint16_t Temp_duty[1] = {0};
uint16_t Null_duty[1] = {0};
uint16_t counter_val = 0;
uint16_t Tps_Value_Max[1] = {0x999};
uint16_t Tps_Value_Min[1] = {0};

static bool g_increaseDuty_flg = true;
static float g_f_pwm_value = 0.0;
static uint16_t g_u16pwm_value_arrar[1] = {0};
static uint32_t g_u32duty;

extern void MCU_DelayMS(uint16_t u16Time);

uint16_t Duty_Arr[193] = 
{
    0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
    3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7,
    7, 7, 8, 8, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 14, 15,
    15, 16, 17, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
    31, 32, 33, 35, 36, 38, 40, 41, 43, 45, 47, 49, 52, 54, 56, 59,
    61, 64, 67, 70, 73, 76, 79, 83, 87, 91, 95, 99, 103, 108, 112,
    117, 123, 128, 134, 140, 146, 152, 159, 166, 173, 181, 189, 197,
    206, 215, 225, 235, 245, 256, 267, 279, 292, 304, 318, 332, 347,
    362, 378, 395, 412, 431, 450, 470, 490, 512, 535, 558, 583, 609,
    636, 664, 693, 724, 756, 790, 825, 861, 899, 939, 981, 1024, 1069,
    1117, 1166, 1218, 1272, 1328, 1387, 1448, 1512, 1579, 1649, 1722,
    1798, 1878, 1961, 2048, 2139, 2233, 2332, 2435, 2543, 2656, 2773,
    2896, 3025, 3158, 3298, 3444, 3597, 3756, 3922, 4095
};

void LightOn_OneCh(uint8_t device_id, uint8_t idx)
{
    uint16_t Full_duty[1] = {DUTY_VALUE_MAX};
    setPWMoneCh(device_id, Full_duty, idx);
}

void LightOff_OneCh(uint8_t device_id, uint8_t idx)
{
    uint16_t Full_duty[1] = {DUTY_VALUE_MAX};
    setPWMoneCh(device_id, Null_duty, idx);
}


void Logo_BreathOn(void)
{
    uint8_t channel = pwm_pal1Configs.pwmChannels[0].channel;
    uint32_t test_duty = 0;
    
    while (test_duty < 29999U)
    {
        PWM_UpdateDuty(&pwm_pal1Instance, channel, test_duty);
        test_duty+=24;
        MCU_DelayMS(1); 
    }
}

void Logo_BreathOff(void)
{
    uint8_t channel = pwm_pal1Configs.pwmChannels[0].channel;
    uint32_t test_duty = 30000;
    
    while (test_duty > 0U)
    {
        PWM_UpdateDuty(&pwm_pal1Instance, channel, test_duty);
        test_duty-=24;
        MCU_DelayMS(1);
    }     
}

void FlowOn_light(void)
{
    uint8_t idx;
    for(idx=23; idx>1; idx-=2)
    {
        LightOn_OneCh(DEVICE_ADDR__1, idx);
        LightOn_OneCh(DEVICE_ADDR__3, idx);
        MCU_DelayMS(67);
    }
    if(idx == 1)
    {
        LightOn_OneCh(DEVICE_ADDR__1, 1);
        LightOn_OneCh(DEVICE_ADDR__3, 1);
        MCU_DelayMS(67);
    }
}

void FlowOff_light(void)
{
    uint8_t idx;
    for(idx=1; idx<24; idx+=2)
    {
        LightOff_OneCh(DEVICE_ADDR__1, idx);
        LightOff_OneCh(DEVICE_ADDR__3, idx);
        MCU_DelayMS(100);
    }
}


void test_929120(void)
{
    uint8_t idx;
    for(idx=0; idx<10; idx++)
    {
        LightOn_OneCh(1, idx);
    }
}

static uint8_t flg = 0;
void Anm_1(void)
{
    uint8_t idx;
     if (flg == 0)
     {
        Logo_BreathOn();
        FlowOn_light();
        flg = 1;
        MCU_DelayMS(500);
     }
     else
     {
        FlowOff_light();
        Logo_BreathOff();
        flg = 0;
        MCU_DelayMS(500);
     }
}

void Anm_2(void)
{
    uint8_t channel = pwm_pal1Configs.pwmChannels[0].channel;

    if (g_increaseDuty_flg == true)
    {
        g_f_pwm_value+=4.914; //2457/500=4.914
        g_u16pwm_value_arrar[0] = (uint16_t)g_f_pwm_value;
        g_u32duty+=60;

        if(g_f_pwm_value >= 2457)
        {
            g_increaseDuty_flg = false;
        }

        if (g_u16pwm_value_arrar[0] < 2457)
        {
            setPWMAllCh(1, g_u16pwm_value_arrar, TRUE);
            setPWMAllCh(3, g_u16pwm_value_arrar, TRUE);
        }
        else
        {
            setPWMAllCh(1, Tps_Value_Max, TRUE);
            setPWMAllCh(3, Tps_Value_Max, TRUE);
        }
        PWM_UpdateDuty(&pwm_pal1Instance, channel, g_u32duty);
        MCU_DelayMS(1);         
    }
    if (g_increaseDuty_flg == false)
    {
        g_f_pwm_value-=4.914;
        g_u16pwm_value_arrar[0] = (uint16_t)g_f_pwm_value;
        g_u32duty-=60;

        if(g_f_pwm_value <= 4.914)
        {
            g_increaseDuty_flg = true;
        }
        
        if (g_u16pwm_value_arrar[0] > 4)
        {
            setPWMAllCh(1, g_u16pwm_value_arrar, TRUE);
            setPWMAllCh(3, g_u16pwm_value_arrar, TRUE);
        }
        else
        {
            setPWMAllCh(1, Tps_Value_Min, TRUE);
            setPWMAllCh(3, Tps_Value_Min, TRUE);
        }
        PWM_UpdateDuty(&pwm_pal1Instance, channel, g_u32duty);
        MCU_DelayMS(1);         
    } 
}

void Anm_Run(void)
{
    uint16_t buff_on[1] = {0x999};
    uint16_t buff_off[1] = {0};

    if (g_u8_RunEnabled_btn1 == 1)
    {
        //10%
        PWM_UpdateDuty(&pwm_pal1Instance, 5, 3000);
    }
    else if(g_u8_RunEnabled_btn1 == 2)
    {
        //20%
        PWM_UpdateDuty(&pwm_pal1Instance, 5, (3000*2));
    }
    else if(g_u8_RunEnabled_btn1 == 3)
    {
        //30%
        PWM_UpdateDuty(&pwm_pal1Instance, 5, (3000*3));
    }
    else if(g_u8_RunEnabled_btn1 == 4)
    {
        //40%
        PWM_UpdateDuty(&pwm_pal1Instance, 5, (3000*4));
    }
    else if(g_u8_RunEnabled_btn1 == 5)
    {
        //50%
        PWM_UpdateDuty(&pwm_pal1Instance, 5, (3000*5));
    }
    else if(g_u8_RunEnabled_btn1 == 6)
    {
        //60%
        PWM_UpdateDuty(&pwm_pal1Instance, 5, (3000*6));
    }
    else if(g_u8_RunEnabled_btn1 == 7)
    {
        //70%
        PWM_UpdateDuty(&pwm_pal1Instance, 5, (3000*7));
    }
    else if(g_u8_RunEnabled_btn1 == 8)
    {
        //80%
        PWM_UpdateDuty(&pwm_pal1Instance, 5, (3000*8));
    }
    else if(g_u8_RunEnabled_btn1 == 9)
    {
        //90%
        PWM_UpdateDuty(&pwm_pal1Instance, 5, (3000*9));
    }
    else if(g_u8_RunEnabled_btn1 == 10)
    {
        //100%
        PWM_UpdateDuty(&pwm_pal1Instance, 5, (3000*10));
    }
    else if(g_u8_RunEnabled_btn1 == 11)
    {
        PWM_UpdateDuty(&pwm_pal1Instance, 5, 0);
        g_u8_pressed_btn1 = 0;
        g_u8_RunEnabled_btn1 = 0;
    }

    if (g_u8_RunEnabled_btn2 == 1)
    {
        //btn2_1
        for(uint8_t i=0; i<24; i+=2)
        {
            setPWMoneCh(1, buff_on, i);
            setPWMoneCh(3, buff_on, i);
        }
    }
    else if(g_u8_RunEnabled_btn2 == 2)
    {
        //btn2_2
        for(uint8_t i=0; i<24; i+=2)
        {
            setPWMoneCh(1, buff_off, i);
            setPWMoneCh(3, buff_off, i);
        }
        g_u8_pressed_btn2 = 0;
        g_u8_RunEnabled_btn2 = 0;
    }

    if (g_u8_RunEnabled_btn3 == 1)
    {
        //btn3_1
        Anm_1();
    }
    else if(g_u8_RunEnabled_btn3 == 2)
    {
        //btn3_2
        PWM_UpdateDuty(&pwm_pal1Instance, 5, 0);
        setPWMAllCh(1, buff_off, TRUE);
        setPWMAllCh(3, buff_off, TRUE);
        g_u8_pressed_btn3 = 0;
        g_u8_RunEnabled_btn3 = 0;
        flg = 0;
    }

    if (g_u8_RunEnabled_btn4 == 1)
    {
        //btn4_1
        Anm_2();
    }
    else if(g_u8_RunEnabled_btn4 == 2)
    {
        //btn4_2
        PWM_UpdateDuty(&pwm_pal1Instance, 5, 0);
        setPWMAllCh(1, buff_off, TRUE);
        setPWMAllCh(3, buff_off, TRUE);
        g_increaseDuty_flg = true;
        g_f_pwm_value = 0;
        g_u32duty = 0;
        g_u8_pressed_btn4 = 0;
        g_u8_RunEnabled_btn4 = 0;
    }
}

